package com.example.threadcode.basicLeverOne.v1;

public class ThreadDemo {

    public static void main(String[] args) {
//       命令行参数
        boolean isDaemon = args.length != 0;

        Runnable r =  new Runnable() {
            @Override
            public void run() {
//                获取当前线程
                Thread thread = Thread.currentThread();
                while (true) {
                    System.out.printf("%s is %s alive and in %s " +
                                    "state%n",
                                    thread.getName(),
                                    thread.isAlive(),
                                    thread.getState());
                }
            }
        };
        Thread t1 = new Thread(r,"thd1");
        if (isDaemon) {
            t1.setDaemon(true);
        }
        System.out.printf("%s is %salive and in %s state%n",
                t1.getName(),
                t1.isAlive() ? "" : "not ",
                t1.getState());
        Thread t2 = new Thread(r);
        t2.setName("thd2");
        if (isDaemon) {
            t2.setDaemon(true);
        }
        System.out.printf("%s is %salive and in %s state%n",
                t2.getName(),
                t2.isAlive() ? "" : "not ",
                t2.getState());
        t1.start();
        t2.start();
    }
}
